package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import academic.concordia.bankofquebec.bean.authentication.MyAuthenticationToken;
import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.AccountAttributes;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Client;

@ManagedBean
@SessionScoped
public class ProductManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SessionData sessionData;

	private List<BankAccount> products;
	
	private BankAccount selectedProduct;
	
	@ManagedProperty(value="#{BankAccountBean}")
	IBankAccountServices bankAccountBean;

	public void updateProductsList() {
		System.out.println("sessionData: " + sessionData);
		if (sessionData != null) {
			products = sessionData.getClient().getProducts();
			System.out.println("products: " + products.size());
		} else {
			SecurityContext context = SecurityContextHolder.getContext();
			Authentication authentication = context.getAuthentication();
			Client client = null;
			if (authentication instanceof MyAuthenticationToken) {
				client = ((MyAuthenticationToken) authentication).getClient();
				products = client.getProducts();
				System.out.println("products: " + products.size());
			}
		}
	}

	public String getProductInformation() {
		
		String accountInfo = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("accountInfo");
		String accountInfoData[] = accountInfo.split("-");
		String accountNumber = accountInfoData[1];
		
		this.selectedProduct = bankAccountBean.getAccountById(accountNumber);
		System.out.println("Account Type: " + selectedProduct.getType() + selectedProduct.getAccountNumber());
		
		if(this.selectedProduct.getType().equals(AccountAttributes.SAVINGS_ACCOUNT.name()))
			return "savings_account";
		else if(this.selectedProduct.getType().equals(AccountAttributes.CHECKINGS_ACCOUNT.name()))
			return "checkings_account";
		else if(this.selectedProduct.getType().equals(AccountAttributes.CREDIT_CARD.name()))
			return "creditcard_account";
		else if(this.selectedProduct.getType().equals(AccountAttributes.MUTUAL_FUNDS.name()))
			return "mutualfunds_account";
		else if(this.selectedProduct.getType().equals(AccountAttributes.GIC.name()))
			return "gic_account";
		return "";
	}

	/*
	 * public ProductManagedBean() {
	 * 
	 * System.out.println("sessionData: " + sessionData); if(sessionData !=
	 * null) { products = sessionData.getClient().getProducts();
	 * System.out.println("products: " + products.size()); } }
	 */

	public List<BankAccount> getProducts() {
		return products;
	}

	public void setProducts(List<BankAccount> products) {
		this.products = products;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	@Autowired
	public void setSessionData(SessionData sessionData) {
		System.out.println("1. sessionData: " + sessionData);
		this.sessionData = sessionData;
	}

	public BankAccount getSelectedProduct() {
		return selectedProduct;
	}

	public void setSelectedProduct(BankAccount selectedProduct) {
		this.selectedProduct = selectedProduct;
	}
	
	public String formatAccountNumber(String accountNumber) {
		String protectedNumber = accountNumber.substring(0, accountNumber.length() - 4).replaceAll("[0-9]*", "*");
		return protectedNumber + accountNumber.substring(accountNumber.length() - 4);
	}

	public IBankAccountServices getBankAccountBean() {
		return bankAccountBean;
	}

	public void setBankAccountBean(IBankAccountServices bankAccountBean) {
		this.bankAccountBean = bankAccountBean;
	}
}
